home *** CD-ROM | disk | FTP | other *** search
- Path: Rezonet.net!news
- From: ray@ultimate-tech.com (Ray Dunn)
- Newsgroups: comp.lang.c
- Subject: Re: quick decision: is n a power of 2?
- Date: 25 Jan 1996 16:24:50 GMT
- Organization: Ultimate Technographics Inc.
- Message-ID: <4e8asi$ehg@ns.RezoNet.NET>
- References: <Pine.OSF.3.91.960119114608.18779E-100000@io.UWinnipeg.ca> <4e1aeb$1gl8@cymbal.aix.calpoly.edu> <822516891snz@genesis.demon.co.uk>
- NNTP-Posting-Host: 204.19.230.7
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-Newsreader: WinVN 0.99.7
-
- In referenced article, Lawrence Kirby says...
- >>int is_power_of_two(int k) { /* 1.0 1.0 1.0 1.0 */
- >> if (k <= 0) return 0;
- >> return (!(k & (k-1)));
- >>}
- >>
- >>int is_power_of_two(int k) { /* 1.1 1.0 1.1 1.0 */
- >> if (k <= 0) return 0;
- >> return ((k & (k-1)) == 0);
- >>}
- >
- >I wouldn't be too impressed these days with a compiler that generates
- >less efficient code for ((k & (k-1) == 0) than (!(k & (k-1))
-
- Yes, that was my reaction too, but looking at it, the differences are
- in the noise level for this sort of timing which usually varies quite a
- bit - especially when using a test that only runs for 1 second.
- --
- Ray Dunn (opinions are my own) | Phone: (514) 938 9050
- Montreal | Phax : (514) 938 5225
- ray@ultimate-tech.com | Home : (514) 630 3749
-
-